package io.github.talelin.latticy.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.github.talelin.core.annotation.AdminRequired;
import io.github.talelin.core.annotation.LoginRequired;
import io.github.talelin.latticy.dto.PointsAdjustDTO;
import io.github.talelin.latticy.dto.PointsRecordDTO;
import io.github.talelin.latticy.model.PointsRecordDO;
import io.github.talelin.latticy.service.PointsRecordService;
import io.github.talelin.latticy.vo.PointsRecordVO;
import io.github.talelin.latticy.vo.PointsStatsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 积分管理控制器
 * @author talelin
 */
@RestController
@RequestMapping("/v1/points")
@Validated
public class PointsController {

    @Autowired
    private PointsRecordService pointsRecordService;

    /**
     * 增加积分
     */
    @PostMapping("/add")
    public PointsRecordDO addPoints(@Valid @RequestBody PointsRecordDTO dto) {
        return pointsRecordService.addPoints(dto);
    }

    /**
     * 扣除积分
     */
    @PostMapping("/deduct")
    public PointsRecordDO deductPoints(@Valid @RequestBody PointsRecordDTO dto) {
        return pointsRecordService.deductPoints(dto);
    }

    /**
     * 管理员调整积分
     */
    @PostMapping("/adjust")
    @AdminRequired
    public PointsRecordDO adjustPoints(@Valid @RequestBody PointsAdjustDTO dto) {
        return pointsRecordService.adjustPoints(dto);
    }

    /**
     * 获取积分记录详情
     */
    @GetMapping("/record/{id}")
    public PointsRecordDO getRecordDetail(@PathVariable @NotNull Integer id) {
        return pointsRecordService.getRecordDetail(id);
    }

    /**
     * 分页获取用户积分记录
     */
    @GetMapping("/user/{userId}/records")
    public IPage<PointsRecordVO> getUserRecords(
            @PathVariable @NotNull Long userId,
            @RequestParam(defaultValue = "1") @Min(1) Integer page,
            @RequestParam(defaultValue = "20") @Min(1) Integer count,
            @RequestParam(required = false) String type,
            @RequestParam(required = false) String source) {
        return pointsRecordService.getUserRecords(userId, page, count, type, source);
    }

    /**
     * 获取用户积分统计信息
     */
    @GetMapping("/user/{userId}/stats")
    public PointsStatsVO getUserPointsStats(@PathVariable @NotNull Long userId) {
        return pointsRecordService.getUserPointsStats(userId);
    }

    /**
     * 获取即将过期的积分记录
     */
    @GetMapping("/user/{userId}/expiring")
    public List<PointsRecordDO> getExpiringPoints(@PathVariable @NotNull Long userId) {
        return pointsRecordService.getExpiringPoints(userId);
    }

    /**
     * 处理积分过期（定时任务调用）
     */
    @PostMapping("/handle-expired")
    public String handleExpiredPoints() {
        pointsRecordService.handleExpiredPoints();
        return "积分过期处理完成";
    }

    /**
     * 检查积分余额是否足够
     */
    @GetMapping("/user/{userId}/check-balance")
    public boolean checkPointsBalance(
            @PathVariable @NotNull Long userId,
            @RequestParam @NotNull @Min(1) Integer requiredPoints) {
        return pointsRecordService.checkPointsBalance(userId, requiredPoints);
    }

    /**
     * 获取用户当前积分余额
     */
    @GetMapping("/user/{userId}/balance")
    public Integer getCurrentBalance(@PathVariable @NotNull Long userId) {
        return pointsRecordService.getCurrentBalance(userId);
    }

    /**
     * 根据订单ID查询积分记录
     */
    @GetMapping("/order/{orderId}/records")
    public List<PointsRecordDO> getRecordsByOrderId(@PathVariable @NotNull Long orderId) {
        return pointsRecordService.getRecordsByOrderId(orderId);
    }

    /**
     * 获取用户积分列表（分页）
     */
    @GetMapping("/users")
    @AdminRequired
    public IPage<PointsStatsVO> getUserPointsList(
            @RequestParam(defaultValue = "1") @Min(1) Integer page,
            @RequestParam(defaultValue = "20") @Min(1) Integer count,
            @RequestParam(required = false) String username,
            @RequestParam(required = false) Integer minPoints,
            @RequestParam(required = false) Integer maxPoints,
            @RequestParam(required = false) String status) {
        return pointsRecordService.getUserPointsList(page, count, username, minPoints, maxPoints, status);
    }

    /**
     * 初始化用户积分（为所有用户设置默认积分）
     */
    @PostMapping("/init-users-points")
    @AdminRequired
    public String initUsersPoints(@RequestParam(defaultValue = "1000") Integer defaultPoints) {
        return pointsRecordService.initUsersPoints(defaultPoints);
    }


}
